package ai.people.netmon.facebook.probe.service.impl;

import ai.people.netmon.facebook.probe.mapper.FacebookGroupMapper;
import ai.people.netmon.facebook.probe.service.FacebookGroupService;
import ai.people.netmon.framework.domain.facebook.probe.entity.FacebookGroup;
import ai.people.netmon.framework.domain.facebook.probe.request.QueryRequest;
import ai.people.netmon.framework.model.response.PageResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author LiGuanNan
 * @date 2022/7/18 15:05
 */
@Service
public class FacebookGroupServiceImpl extends ServiceImpl<FacebookGroupMapper, FacebookGroup> implements FacebookGroupService {
    /**
     * 分页查询页面
     *
     * @param request 请求
     * @return {@link PageResponse}<{@link FacebookGroup}>
     */
    @Override
    public PageResponse<FacebookGroup> queryPage(QueryRequest request) {
        LambdaQueryWrapper<FacebookGroup> queryWrapper = new LambdaQueryWrapper<FacebookGroup>()
                .like(StringUtils.isNotBlank(request.getSearchKeywords()), FacebookGroup::getGroupName, request.getSearchKeywords());
        long total = this.count(queryWrapper);
        if(total > 0){
            Page<FacebookGroup> pageParam = new Page<>(request.getOriginPageNo(), request.getPageSize(), total);
            queryWrapper.orderByDesc(FacebookGroup::getTs);
            Page<FacebookGroup> page = this.page(pageParam, queryWrapper);
            return PageResponse.builder(page);
        }
        return PageResponse.builder(Lists.newArrayList());
    }
}
